﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ATEC.ATDataCoreNET;
using ATEC.ATDataCoreNET.ATObjects;
using SSDPNET;
using SZEWAlignmentNet.AlignmentBaseClass;
using SZEWAlignmentNet.AlignmentInterface;

namespace DoModelExample
{
    public class SetAllignmentInfo
    {
        public static void SetInfo(ATObject obj)
        {
            ComponentProp info = new ComponentProp();
            SZEWAlignmentNet.AlignmentBaseClass.AlignmentInfo alignInfo = new SZEWAlignmentNet.AlignmentBaseClass.AlignmentInfo();
            SZEWAlignmentInterface.QueryObjectAlignmentInfo(out alignInfo, obj);

            if(alignInfo == null)
            {
                return;
            }
            AlignmentInfo roadinfo = new AlignmentInfo();
            roadinfo.RoadId = alignInfo.Id;
            roadinfo.RoadName = alignInfo.Name;
            info.RoadInfo = roadinfo;

            CreateComponentExample.componentModel.component = info;
        }

    }

    public class AlignmentSelectionTool : ATObjectSetCmd
    {
        public string m_CmdName = "";
        public string m_CmdStatus = "";
        Action m_pFun = null; 

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="toolName"></param>
        AlignmentSelectionTool(string toolName, Action method) : base()
        {
            ViewManager.ActiveView(ViewIndex.ViewIndex_Horizon);
            m_CmdName = toolName;
            m_pFun = method;
        }

        protected override bool OnInstall()
        {
            return base.OnInstall();
        }

        protected override bool OnPostLocate(HitPath path, out string cantAcceptReason)
        {
            cantAcceptReason = "";
            ATObject obj = path.GetHeadObject();
            if (obj == null) // 对象无效
            {
                cantAcceptReason = "对象无法使用该工具";
                return false;
            }

            ATProject masterATProject = SSDPNET.ATObjFuncs.ProjectObjGetMasterProject();
            ATSceneRef pDefaultMd = SZEWAlignmentInterface.GetHoriScene(masterATProject); // -- 看类名，平面Scene.
            string pName = pDefaultMd.GetATScene().SceneName;
            string curSceneName = "三维设计";

            if (curSceneName != pName) // 对象不在平面scene
            {
                cantAcceptReason = "对象无法使用该工具";
                return false;
            }

            //SetAllignmentInfo.SetInfo(obj);

            return true;
        }

        // 命令启动后，初始化工作
        protected override void OnPostInstall()
        {
            // 启用Auto-Locate同时关闭AccuDraw
            ObjectEdit.LocateEnabled = true;           
            ATState.AccuDrawSetEnabledState(false);
            // 设置初始状态，并使用动态帧更新.
            m_CmdStatus = "请选择: 二维路线， 右键退出.";
            StartRTUpdate();
        }

        protected override bool SetupForModify(ATBtnEvent ev, bool isRTUpdate)
        {
            return base.SetupForModify(ev, isRTUpdate);
        }

        protected override void ExitCmd()
        {
            base.ExitCmd();
        }

        // 鼠标左键事件
        protected override bool OnDataButton(ATBtnEvent ev)
        {
            return base.OnDataButton(ev);
        }

        protected override void OnRTUpdateMode(ATBtnEvent ev)
        {
            ATNotificationMGR.OutputPrompt(m_CmdName);
            ATNotificationMGR.OutputPrompt(m_CmdStatus);
        }

        // 鼠标右键，结束事件
        protected override bool OnResetButton(ATBtnEvent ev)
        {
            ExitCmd();
            return true;
        }

        protected override bool OnModifyComplete(ATBtnEvent ev)
        {
            if (!ObjectSelectSet.IsEmpty)
            {
                ATObject obj = ObjectSelectSet.GetFirst();
                //这里把数据保存给构件
                SetAllignmentInfo.SetInfo(obj);
                m_pFun();
            }

            return true; // restart tool
        }

        protected override void OnReinitialize()
        {
            base.OnReinitialize();
        }

        protected override bool WantRTUpdate()
        {
            return base.WantRTUpdate();
        }

        protected override void OnRestartCmd()
        {
            Start(m_CmdName, m_pFun);
        }

        public static void Start(string toolId, Action startCreateMethod)
        {
            AlignmentSelectionTool tool = new AlignmentSelectionTool(toolId, startCreateMethod);
            tool.InstallCmd();
        }

        public override StatusInt OnObjectModify(ATObject @object)
        {
            return StatusInt.Error;
        }
    }
}
